<template>
  <div class="shopping-cart-container">
    <div class="cart-header">
      <h2 class="cart-title">精品购物车</h2>
      <p class="cart-subtitle">优雅购物体验，尽在指尖</p>
    </div>
    
    <div class="table-wrapper">
      <table class="elegant-table">
        <thead>
          <tr class="table-header">
            <th class="header-cell">商品ID</th>
            <th class="header-cell">商品名称</th>
            <th class="header-cell">单价</th>
            <th class="header-cell">数量</th>
            <th class="header-cell">小计</th>
          </tr>
        </thead>
        <tbody>
          <tr 
            v-for="(item, index) in goods" 
            :key="index" 
            class="table-row"
            :class="{ 'even-row': index % 2 === 0 }"
          >
            <td class="data-cell id-cell">{{ item.id }}</td>
            <td class="data-cell name-cell">{{ item.name }}</td>
            <td class="data-cell price-cell">¥{{ item.price.toLocaleString() }}</td>
            <td class="data-cell quantity-cell">
              <div class="quantity-controls">
                <button 
                  class="quantity-btn decrease-btn" 
                  @click="sub(index)"
                  :disabled="item.count <= 1"
                >
                  <span class="btn-icon">−</span>
                </button>
                <input
                  type="number"
                  :value="item.count"
                  @input="handleQuantityInput($event, index)"
                  @blur="validateQuantity(index)"
                  @keyup.enter="validateQuantity(index)"
                  min="1"
                  max="999"
                  class="quantity-input"
                >
                <button 
                  class="quantity-btn increase-btn" 
                  @click="add(index)"
                >
                  <span class="btn-icon">+</span>
                </button>
              </div>
            </td>
            <td class="data-cell total-cell">¥{{ (item.price * item.count).toLocaleString() }}</td>
          </tr>
        </tbody>
      </table>
    </div>

    <div class="cart-footer">
      <div class="total-section">
        <span class="total-label">商品总数：</span>
        <span class="total-value">{{ totalItems }} 件</span>
      </div>
      <div class="total-section">
        <span class="total-label">订单总额：</span>
        <span class="total-value grand-total">¥{{ grandTotal }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ElegantShoppingCart',
  data() {
    return {
      goods: [
        { id: 1, name: 'iPhone 15 Pro', price: 8888, count: 1 },
        { id: 2, name: 'iPad Air', price: 5888, count: 1 },
        { id: 3, name: 'MacBook Pro', price: 15888, count: 1 },
        { id: 4, name: 'Apple Watch', price: 3888, count: 1 },
        { id: 5, name: 'AirPods Pro', price: 2188, count: 1 }
      ]
      //total:0
    }
  },
  methods: {
    sub(index) {
      if (this.goods[index].count > 1) {
        this.goods[index].count--
        grandTotal()
      }
    },
    add(index) {
      this.goods[index].count++
      grandTotal()
    },
    handleQuantityInput(event, index) {
      // 获取输入值并转换为数字
      const value = parseInt(event.target.value)
      
      // 如果输入为空或无效，暂时不更新数据，保持原值
      if (isNaN(value)) {
        return
      }
      
      // 实时更新数量，但在失去焦点时进行最终验证
      this.goods[index].count = value
    },
    validateQuantity(index) {
      // 验证输入的数量，确保是有效数字且在合理范围内
      let value = this.goods[index].count
      
      // 如果值不是数字或小于1，设为1
      if (isNaN(value) || value < 1) {
        this.goods[index].count = 1
        return
      }
      
      // 限制最大数量，避免过大数值（实际场景中可能根据库存限制）
      if (value > 999) {
        this.goods[index].count = 999
      }
      
      // 确保是整数
      this.goods[index].count = Math.floor(value)
    }
  },
  computed: {
    totalItems() {
      return this.goods.reduce((total, item) => total + item.count, 0)
    },
    grandTotal() {
      return this.goods.reduce((total, item) => total + (item.price * item.count), 0)
     }
  }
}
</script>

<style scoped>
.shopping-cart-container {
  max-width: 1200px;
  margin: 40px auto;
  padding: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.cart-header {
  text-align: center;
  margin-bottom: 40px;
  color: white;
}

.cart-title {
  font-size: 2.5em;
  font-weight: 300;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.cart-subtitle {
  font-size: 1.1em;
  opacity: 0.9;
  font-weight: 300;
}

.table-wrapper {
  background: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
}

.elegant-table {
  width: 100%;
  border-collapse: collapse;
  background: white;
}

.table-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.header-cell {
  padding: 20px 15px;
  color: white;
  font-weight: 500;
  font-size: 1.1em;
  text-align: center;
  border-bottom: 2px solid rgba(255, 255, 255, 0.1);
}

.table-row {
  transition: all 0.3s ease;
  border-bottom: 1px solid #f0f0f0;
}

.table-row:hover {
  background-color: #f8f9fa;
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.even-row {
  background-color: #fafafa;
}

.data-cell {
  padding: 20px 15px;
  text-align: center;
  font-size: 1em;
  color: #555;
  border: none;
}

.id-cell {
  font-weight: 600;
  color: #667eea;
}

.name-cell {
  font-weight: 500;
  color: #333;
}

.price-cell, .total-cell {
  font-weight: 600;
  color: #2c3e50;
}

.quantity-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
}

.quantity-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-size: 1.2em;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.quantity-btn:hover:not(:disabled) {
  transform: scale(1.1);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.quantity-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
  box-shadow: none;
}

.quantity-input {
  width: 80px;
  height: 40px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  text-align: center;
  font-size: 1.1em;
  font-weight: 500;
  color: #333;
  background: #fafafa;
  transition: all 0.3s ease;
  outline: none;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05);
}

.quantity-input:focus {
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1), inset 0 2px 4px rgba(0, 0, 0, 0.05);
  transform: scale(1.02);
}

.quantity-input:hover {
  border-color: #ccc;
  background: white;
}

.quantity-input:invalid {
  border-color: #e74c3c;
  background: #fff5f5;
}

.btn-icon {
  line-height: 1;
}

.cart-footer {
  background: white;
  padding: 25px;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.total-section {
  display: flex;
  align-items: center;
  gap: 15px;
}

.total-label {
  font-size: 1.2em;
  color: #666;
  font-weight: 500;
}

.total-value {
  font-size: 1.4em;
  font-weight: 600;
  color: #333;
}

.grand-total {
  color: #e74c3c;
  font-size: 1.6em;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .shopping-cart-container {
    margin: 20px;
    padding: 20px;
  }
  
  .cart-title {
    font-size: 2em;
  }
  
  .header-cell, .data-cell {
    padding: 15px 10px;
    font-size: 0.9em;
  }
  
  .quantity-controls {
    gap: 10px;
  }
  
  .quantity-input {
    width: 60px;
    height: 35px;
    font-size: 1em;
  }
  
  .quantity-btn {
    width: 35px;
    height: 35px;
    font-size: 1em;
  }
  
  .cart-footer {
    flex-direction: column;
    gap: 15px;
    text-align: center;
  }
}

/* 移除输入框的数字增减按钮的默认样式 */
.quantity-input::-webkit-outer-spin-button,
.quantity-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.quantity-input[type=number] {
  -moz-appearance: textfield;
}
</style>
